package com.ruoyi.contractHomepage.service.impl;

import com.ruoyi.contractHomepage.domain.DocContractHome;
import com.ruoyi.contractHomepage.domain.vo.DoctorVo;
import com.ruoyi.contractHomepage.mapper.DocContractHomeMapper;
import com.ruoyi.contractHomepage.service.IDocContractHomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DocContractHomeImpl implements IDocContractHomeService {

    @Autowired
    private DocContractHomeMapper docContractHomeMapper;

    /**
     * 查询指定团队及其医生信息
     *
     * @param teamId 团队ID
     * @return 团队信息及医生信息
     */
    @Override
    public DocContractHome selectTeamWithDoctor(Long teamId) {
        return docContractHomeMapper.selectTeamWithDoctor(teamId);
    }

    /**
     * 查询所有包含医生的团队信息
     *
     * @return 包含医生的团队列表，每个团队包含对应的医生信息
     */
    @Override
    public List<DocContractHome> selectAllTeamsWithDoctor() {
        return docContractHomeMapper.selectAllTeamsWithDoctor();
    }

    /**
     * 查询所有包含医生的团队信息（拆分Sql）
     *
     * @return 包含医生的团队合同首页信息列表
     */
    @Override
    public List<DocContractHome> selectAllTeamsWithDoctor2() {
        // 查询所有团队信息
        List<DocContractHome> teams = docContractHomeMapper.selectAllTeams();

        // 为每个团队查询医生信息
        for (DocContractHome team : teams) {
            List<DoctorVo> doctors = docContractHomeMapper.selectDoctorsByTeamId(team.getTeamId());
            team.setDoctorVoList(doctors);
        }
        return teams;
    }

    /**
     * 根据团队姓名模糊查询团队含医生
     *
     * @param teamName 团队姓名
     * @return 返回匹配的团队列表
     */
    @Override
    public List<DocContractHome> searchTeamsWithDoctorsByName(String teamName) {
        // 先查询匹配的团队信息
        List<DocContractHome> teams = docContractHomeMapper.searchTeamsByName(teamName);

        // 为每个团队查询医生信息
        for (DocContractHome team : teams) {
            List<DoctorVo> doctors = docContractHomeMapper.selectDoctorsByTeamId(team.getTeamId());
            team.setDoctorVoList(doctors);
        }
        return teams;
    }


}
